package org.budo.alipay.api;

import java.math.BigDecimal;

import org.budo.alipay.util.AlipayUtil;
import org.budo.time.Time;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 */
@Slf4j
public class AliPayApiImpl extends AbstractAliPayApi {
    public void wapPay() {
        String amount = "1";

        String now = Time.now().toString("yyMMddHHmmss");

        String outTradeNo = "order.getMallId()-order.getId()-" + now;
        String subject = "订单-order.getMallId()-order.getId()(alipay_h5)-" + now;

        AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(outTradeNo);
        model.setSubject(subject);

        // 分 转为 元
        String yuan = BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
        model.setTotalAmount(yuan);

        model.setProductCode("QUICK_WAP_WAY");
        alipay_request.setBizModel(model);

        // 设置异步通知地址
        alipay_request.setNotifyUrl("notify_url");

        // 设置同步地址
        alipay_request.setReturnUrl("return_url");

        // 调用SDK生成表单
        AlipayClient alipayClient = this.alipayClient();
        AlipayTradeWapPayResponse pageExecute = AlipayUtil.pageExecute(alipayClient, alipay_request);
        String form = pageExecute.getBody();
        // IoUtil.write(form, Mvcs.getResponseOutputStream());

        System.err.println(form);

        log.info("#112 alipay form=" + form);
    }
}